home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / autopaint / modcolor.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  121 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include "lum.h"
  18. #include "canvas.h"
  19.  
  20. #define NRTABS    20
  21. static int isat;
  22. static unsigned char *rtabs[NRTABS];
  23. static pixel backcol;
  24.  
  25. setbackcolor(col)
  26. pixel *col;
  27. {
  28.     backcol = *col;
  29. }
  30.     
  31. setsaturate(sat)
  32. float sat;
  33. {
  34.     if(sat<0.0)
  35.         sat = 0.0;
  36.     isat = 255*sat;
  37. }
  38.  
  39. setnoisemag(mag)
  40. float mag;
  41. {
  42.     int i, v, maxdel, idel;
  43.     unsigned char *cptr;
  44.        
  45.     if(mag<0.0)
  46.     mag = 0.0;
  47.     if(mag>1.0)
  48.     mag = 1.0;
  49.     if(!rtabs[0]) {
  50.     for(i=0; i<NRTABS; i++)
  51.         rtabs[i] = (unsigned char *)malloc(256*sizeof(char));
  52.     }
  53.     maxdel = 127*mag;
  54.     for(i=0; i<NRTABS; i++) {
  55.     cptr = rtabs[i];
  56.     for(v=0; v<256; v++) {
  57.         idel = maxdel;
  58.         if(v<idel)
  59.         idel = v;
  60.         if((255-v)<idel)
  61.         idel = 255-v;
  62.         idel++;
  63.         if(random()%0x8)
  64.         *cptr++ = v+(random()%idel);
  65.         else
  66.         *cptr++ = v-(random()%idel);
  67.     }
  68.     }
  69. }
  70.  
  71. modifycolor(col)
  72. pixel *col;
  73. {
  74.     long int bw, r, g, b;
  75.     int curtab;
  76.     
  77.     if(colorsame(col,&backcol))
  78.     return;
  79.     r = col->r;
  80.     g = col->g;
  81.     b = col->b;
  82.     bw = ILUM(r,g,b);
  83.     bw = bw*(256-isat);
  84.     r = (bw+isat*r)>>8;
  85.     g = (bw+isat*g)>>8;
  86.     b = (bw+isat*b)>>8;
  87.     if(r<0) r = 0; else if(r>255) r = 255;
  88.     if(g<0) g = 0; else if(g>255) g = 255;
  89.     if(b<0) b = 0; else if(b>255) b = 255;
  90.     curtab = random()%(NRTABS-3);
  91.     r = rtabs[curtab++][r];
  92.     g = rtabs[curtab++][g];
  93.     b = rtabs[curtab++][b];
  94.     col->r = r;
  95.     col->g = g;
  96.     col->b = b;
  97. }
  98.  
  99. colorsame(c1,c2)
  100. pixel *c1, *c2;
  101. {
  102.     if(c1->r != c2->r)
  103.     return 0;
  104.     if(c1->g != c2->g)
  105.     return 0;
  106.     if(c1->b != c2->b)
  107.     return 0;
  108.     return 1;
  109. }
  110.  
  111. int colordelta(c1,c2)
  112. pixel *c1, *c2;
  113. {
  114.     int dr, dg, db;
  115.  
  116.     dr = RINTLUM*(c1->r-c2->r);
  117.     dg = GINTLUM*(c1->g-c2->g);
  118.     db = BINTLUM*(c1->b-c2->b);
  119.     return fsqrt((float)dr*dr+dg*dg+db*db)+0.5;
  120. }
  121.